package TicketingSystem;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class RegistrationFrame extends JFrame {
    public RegistrationFrame() {
        setTitle("账号注册");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new GridLayout(0, 1, 10, 10));
        setResizable(false);

        // 标题面板
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel titleLabel = new JLabel("账号注册");
        titleLabel.setFont(new Font("隶书", Font.BOLD, 30));
        titlePanel.add(titleLabel);

        // 用户名输入面板
        JPanel userPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel userLabel = new JLabel("用户名：");
        userLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JTextField userField = new JTextField(20);
        userPanel.add(userLabel);
        userPanel.add(userField);

        // 密码输入面板
        JPanel passPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel passLabel = new JLabel("密    码：");
        passLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JPasswordField passField = new JPasswordField(20);
        passPanel.add(passLabel);
        passPanel.add(passField);

        // 电话输入面板
        JPanel phonePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel phoneLabel = new JLabel("电    话：");
        phoneLabel.setFont(new Font("楷书", Font.PLAIN, 24));
        JTextField phoneField = new JTextField(20);
        phonePanel.add(phoneLabel);
        phonePanel.add(phoneField);

        // 用户类型选择面板
        JPanel userTypePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel userTypeLabel = new JLabel("用户类型：");
        userTypeLabel.setFont(new Font("楷书", Font.PLAIN, 20));
        JComboBox<String> userTypeComboBox = new JComboBox<>(new String[]{"普通用户", "管理员"});
        userTypePanel.add(userTypeLabel);
        userTypePanel.add(userTypeComboBox);

        // 管理员注册密码输入面板，初始不可见
        JPanel adminPassPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel adminPassLabel = new JLabel("管理员注册密码：");
        adminPassLabel.setFont(new Font("楷书", Font.PLAIN, 20));
        JPasswordField adminPassField = new JPasswordField(14);
        adminPassPanel.add(adminPassLabel);
        adminPassPanel.add(adminPassField);
        adminPassPanel.setVisible(false);

        // 监听用户类型选择，若选择管理员则显示管理员注册密码输入框
        userTypeComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (userTypeComboBox.getSelectedItem().equals("管理员")) {
                    adminPassPanel.setVisible(true);
                } else {
                    adminPassPanel.setVisible(false);
                }
            }
        });

        // 注册按钮面板
        JPanel btnPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton registerBtn = new JButton("注册");
        registerBtn.setFont(new Font("楷书", Font.PLAIN, 24));
        registerBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String username = userField.getText().trim();
                String password = new String(passField.getPassword()).trim();
                String phone = phoneField.getText().trim();
                String userType = (String) userTypeComboBox.getSelectedItem();

                // 验证必要信息是否填写
                if (username.isEmpty()) {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "请输入用户名。");
                    return;
                }
                if (password.isEmpty()) {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "请输入密码。");
                    return;
                }
                if (phone.isEmpty()) {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "请输入电话号码。");
                    return;
                }

                if (userType.equals("管理员")) {
                    String adminPassword = new String(adminPassField.getPassword()).trim();
                    if (adminPassword.isEmpty()) {
                        JOptionPane.showMessageDialog(RegistrationFrame.this, "请输入管理员注册密码。");
                        return;
                    }
                    if (!adminPassword.equals("g8888")) {
                        JOptionPane.showMessageDialog(RegistrationFrame.this, "管理员注册密码错误，请重新输入。");
                        return;
                    }
                }

                User user = new User(username, password, phone, userType);
                boolean isRegistered = UserService.register(user);
                if (isRegistered) {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "注册成功！");
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(RegistrationFrame.this, "该用户名已被占用，请重新输入");
                }
            }
        });
        btnPanel.add(registerBtn);

        // 添加各个面板到窗口
        add(titlePanel);
        add(userPanel);
        add(passPanel);
        add(phonePanel);
        add(userTypePanel);
        add(adminPassPanel);
        add(btnPanel);

        // 设置窗口大小和位置
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
}